<%--
    Document   : addToCart
    Author     : Thomas
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="addBookToCartBean" class="beans_pkg.addBookToCartBean" scope="page"/>
<jsp:setProperty name="addBookToCartBean" property="*"/>

<%
    if(session.getAttribute("cart") == null){
        session.setAttribute("cart", "");
    }
    String book = addBookToCartBean.getISBN().toString();
    String cart = (String) session.getAttribute("cart");
    cart +=  book + ",";
    session.setAttribute("cart", cart);

    if(session.getAttribute("list") != null){
        String list = (String) session.getAttribute("list");
        if(list.contains(book)){

            String[] listContents = list.split(",");
            list = "";
            for(int i=0; i<listContents.length; i++){
                if(!listContents[i].contains(book)){
                    list += listContents[i] + ",";
                }
            }
            if(list == ""){
                session.setAttribute("list", null);
            }
            else{
                session.setAttribute("list", list);
            }

        }
    }
    
    String redirectURL = (String) session.getAttribute("currentPage");
    response.sendRedirect(redirectURL);
%>
